package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.OrderDetail;
import com.cskaoyan.gateway.form.shopping.PageInfo;
import com.cskaoyan.gateway.form.shopping.PageResponse;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@RequestMapping("/shopping")
@Slf4j
public class OrderController {

    @Reference
    OrderCoreService orderCoreService;

    @Reference
    OrderQueryService orderQueryService;


    @PostMapping("/order")
    public ResponseData addOrder(@RequestBody CreateOrderRequest createOrderRequest,
                                 HttpServletRequest servletRequest) {

        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        createOrderRequest.setUserId(uid);
        createOrderRequest.setUniqueKey(UUID.randomUUID().toString());
        log.info("to add order " + createOrderRequest);
        CreateOrderResponse orderResponse = orderCoreService.createOrder(createOrderRequest);
        if (orderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(orderResponse.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(orderResponse.getMsg());
    }

    @GetMapping("/order")
    public ResponseData userOrders(PageInfo pageInfo, HttpServletRequest request) {
        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setPage(pageInfo.getPage());
        orderListRequest.setSize(pageInfo.getSize());
        orderListRequest.setSort(pageInfo.getSort());

        String token = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject tokenJson = JSON.parseObject(token);
        Long uid = tokenJson.getLong("uid");
        orderListRequest.setUserId(uid);

        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);
        if (orderListResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {

            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(orderListResponse.getDetailInfoList());
            pageResponse.setTotal(orderListResponse.getTotal());
            return new ResponseUtil<>().setData(pageResponse);
        }
        return new ResponseUtil<>().setErrorMsg(orderListResponse.getMsg());
    }

    @GetMapping("/order/{id}")
    public ResponseData orderDetail(@PathVariable("id") String orderId) {
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(orderId);

        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(orderDetailRequest);
        if (orderDetailResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderTotal(orderDetailResponse.getPayment());
            orderDetail.setUserId(orderDetailResponse.getUserId());
            orderDetail.setUserName(orderDetailResponse.getBuyerNick());
            orderDetail.setGoodsList(orderDetailResponse.getOrderItemDto());
            orderDetail.setTel(orderDetailResponse.getOrderShippingDto().getReceiverPhone());
            orderDetail.setStreetName(orderDetailResponse.getOrderShippingDto().getReceiverAddress());
            orderDetail.setOrderStatus(orderDetailResponse.getStatus());
            return new ResponseUtil<>().setData(orderDetail);
        }
        return new ResponseUtil<>().setErrorMsg(orderDetailResponse.getMsg());

    }

    //    @PutMapping("/cancelOrder/{id}")
    @RequestMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody OrderDetailInfo orderDetailInfo) {

        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
        cancelOrderRequest.setOrderId(orderDetailInfo.getOrderId());

        System.out.println(orderDetailInfo.getOrderId());

        CancelOrderResponse cancelOrderResponse
                = orderCoreService.cancelOrder(cancelOrderRequest);
        return new ResponseUtil<>().setData(cancelOrderResponse);

    }

    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable String id) {
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id);
        DeleteOrderResponse deleteOrderResponse = orderCoreService.deleteOrder(deleteOrderRequest);
        return new ResponseUtil<>().setData(deleteOrderResponse);
    }


}
